import time
import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Edge()
driver.get("https://www.zhipin.com/")
driver.find_element(By.CSS_SELECTOR, "#wrap > div.column-search-panel.search-panel-new > div > div.search-box > div.search-form > form > div.search-form-con > p > input").send_keys("大数据开发工程师")
driver.find_element(By.CSS_SELECTOR, "#wrap > div.column-search-panel.search-panel-new > div > div.search-box > div.search-form > form > button").click()
time.sleep(15)
weilist = []

for i in range(2):
    mylist = driver.find_elements(By.CSS_SELECTOR, ".search-job-result ul li")
    for li in mylist:
        try:
            llist = [
                li.find_element(By.CSS_SELECTOR, ".job-title.clearfix .job-name").text,
                li.find_element(By.CSS_SELECTOR, ".job-title.clearfix .job-area").text,
                li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .salary").text,
                li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .tag-list").text.replace("\n", "，"),
                li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .info-public").text,
                li.find_element(By.CSS_SELECTOR, ".job-card-right .company-name").text,
                li.find_element(By.CSS_SELECTOR, ".job-card-right ul").text,
                li.find_element(By.CSS_SELECTOR, ".job-card-footer.clearfix ul").text,
                li.find_element(By.CSS_SELECTOR, ".job-card-footer.clearfix .info-desc").text
            ]
            weilist.append(llist)
        except:
            pass
    driver.find_element(By.CSS_SELECTOR, "#wrap > div.page-job-wrapper > div.page-job-inner > div > div.job-list-wrapper > div.search-job-result > div > div > div > a:nth-child(10)").click()
    time.sleep(15)

for jun in weilist:
    connect = pymysql.connect(host="localhost", user="root", password="123456", db="spiderwork")
    course = connect.cursor()
    sql = "INSERT INTO boss直聘 values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')"%(jun[0], jun[1], jun[2], jun[3], jun[4], jun[5], jun[6], jun[7], jun[8])
    try:
        print("正在写入数据！")
        course.execute(sql)
        connect.commit()
    except Exception as e:
        print("数据写入失败！", e)
        connect.rollback()
        connect.close()
